import requests
import os

# 创建目录
os.makedirs('static/js', exist_ok=True)
os.makedirs('static/css', exist_ok=True)

# 定义要下载的文件
files_to_download = [
    {
        'url': 'https://unpkg.com/echarts/dist/echarts.min.js',
        'path': 'static/js/echarts.min.js'
    },
    {
        'url': 'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js',
        'path': 'static/js/jquery.min.js'
    },
    {
        'url': 'https://cdn.bootcdn.net/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css',
        'path': 'static/css/bootstrap.min.css'
    }
]

def download_file(url, path):
    """下载文件"""
    try:
        print(f"正在下载 {url}...")
        response = requests.get(url, timeout=30)
        response.raise_for_status()
        
        with open(path, 'wb') as f:
            f.write(response.content)
        
        print(f"✅ 成功下载到 {path}")
        return True
    except Exception as e:
        print(f"❌ 下载失败 {url}: {e}")
        return False

def main():
    """主函数"""
    print("开始下载静态资源文件...")
    
    success_count = 0
    for file_info in files_to_download:
        if download_file(file_info['url'], file_info['path']):
            success_count += 1
    
    print(f"\n下载完成！成功下载 {success_count}/{len(files_to_download)} 个文件")
    
    # 验证文件是否存在
    print("\n验证文件:")
    for file_info in files_to_download:
        if os.path.exists(file_info['path']):
            size = os.path.getsize(file_info['path'])
            print(f"✅ {file_info['path']} ({size} 字节)")
        else:
            print(f"❌ {file_info['path']} (文件不存在)")

if __name__ == "__main__":
    main()